﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Localisation;
using Rio.Framework.Pages.AppCode.Enums;
using Rio.Framework.Pages.Context;
using Rio.Framework.Pages.Controls.BaseClass;
using Rio.Framework.Pages.Helpers;

namespace Rio.Framework.Pages.Controls
{
    /// <summary>
    /// Organization - FolderType navigation
    /// </summary>
    public partial class UCOrganizationTypePanel : OrganizationTypePanelBase
    {
        #region Attributs

        //private User _CurrentUser;

        #endregion

        #region DependencyProperties

        /// <summary>
        /// Get or Set Organization Identifier
        /// </summary>
        public long SelectedOrganizationIdentifier
        {
            get { return (long)GetValue(SelectedOrganizationIdentifierProperty); }
            set
            {
                SetValue(SelectedOrganizationIdentifierProperty, value);
                OnNotifyPropertyChanged("SelectedOrganizationIdentifier");
            }
        }
        public static readonly DependencyProperty SelectedOrganizationIdentifierProperty =
            DependencyProperty.Register("SelectedOrganizationIdentifier", typeof(long), typeof(UCOrganizationTypePanel), null);

        /// <summary>
        /// Get or Set FolderTYpe Identifier
        /// </summary>
        public long SelectedFolderIdentifier
        {
            get { return (long)GetValue(SelectedFolderIdentifierProperty); }
            set
            {
                SetValue(SelectedFolderIdentifierProperty, value);
                OnNotifyPropertyChanged("SelectedFolderIdentifier");
            }
        }
        public static readonly DependencyProperty SelectedFolderIdentifierProperty =
            DependencyProperty.Register("SelectedFolderIdentifier", typeof(long), typeof(UCOrganizationTypePanel), null);

        #endregion

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnNotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public delegate void OrganizationSelectedEventHandler(object sender, OrganizationSelectedEventArgs args);
        public event OrganizationSelectedEventHandler OrganizationSelected;
        private void OnOrganizationSelected()
        {
            if (OrganizationSelected != null)
            {
                OrganizationSelected(this, new OrganizationSelectedEventArgs(SelectedOrganization));
            }
        }

        public delegate void FolderSelectedEventHandler(object sender, FolderSelectedEventArgs args);
        public event FolderSelectedEventHandler FolderSelected;
        private void OnFolderSelected()
        {
            if (FolderSelected != null)
            {
                FolderSelected(this, new FolderSelectedEventArgs(SelectedFolderType));
            }
        }

        public event EventHandler FolderTypeSourceChanged;
        private void OnFolderTypeSourceChanged()
        {
            if (FolderTypeSourceChanged != null)
            {
                FolderTypeSourceChanged(this, new EventArgs());
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Used by design time
        /// </summary>
        public UCOrganizationTypePanel()
        {
            InitializeComponent();

            labelOrganization.Text = Resx.GetLabel(ResxIndexPanel.ORGANIZATION);
            labelFolderType.Text = Resx.GetLabel(ResxIndexPanel.FOLDER_TYPE);

            cbFolderType.ItemsSourceChanged += cbFolderType_ItemsSourceChanged;
        }

        /// <summary>
        /// Used in code
        /// </summary>
        /// <param name="CurrentUser">The current User instance</param>
        /// <param name="Mode">Mode used by control</param>
        public UCOrganizationTypePanel(AppDisplayMode Mode)
            : this()
        {
            InitControl(Mode);
        }

        #endregion

        #region Public functions

        /// <summary>
        /// Used to initialize control
        /// </summary>
        /// <param name="currentUser">The current User instance</param>
        /// <param name="Mode">Mode used by control</param>
        public void InitControl(AppDisplayMode Mode)
        {
            this.Mode = Mode;
            InitOrganizationData();
        }

        /// <summary>
        /// Used to clear select organization
        /// </summary>
        public void ClearSelectedOrganization()
        {
            cbOrganization.SelectedIndex = -1;
        }

        /// <summary>
        /// Used to clear selected folder type
        /// </summary>
        public void ClearSelectedFolder()
        {
            cbFolderType.SelectedIndex = -1;
        }

        /// <summary>
        /// Enable or disable combobox
        /// </summary>
        /// <param name="value">Boolean value</param>
        public void SetIsEnabledComboBoxProperty(bool value)
        {
            cbOrganization.IsEnabled = value;
            cbFolderType.IsEnabled = value;
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Handles the ItemsSourceChanged event of the cbFolderType control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void cbFolderType_ItemsSourceChanged(object sender, EventArgs e)
        {
            OnFolderTypeSourceChanged();
        }

        /// <summary>
        /// Init user's organization list
        /// </summary>
        private void InitOrganizationData()
        {
            List<Group> L_oListGroup = new List<Group>(base.CurrentUser.GroupList);
            ObservableCollection<Organization> tmpOrganizations = new ObservableCollection<Organization>();
            OrganizationComparer L_oOrganizationComparer = new OrganizationComparer();
            Organization _SelectedOrg = null;

            foreach (Group g in L_oListGroup)
            {
                foreach (Organization org in g.OrganizationList)
                {
                    if (!tmpOrganizations.Contains(org, L_oOrganizationComparer))
                    {
                        if (GlobalWebContext.OrganizationHaveFolderTypeRight(org, Mode))
                        {
                            tmpOrganizations.Add(org);
                            if (org.Identifier == GlobalWebContext.CurrentUser.CTX_DefaultOrganization && Mode != AppDisplayMode.AddDocument)
                                _SelectedOrg = org;
                        }
                    }

                }
            }

            Organizations = new ObservableCollection<Organization>(tmpOrganizations);

            //Databinding & default selection
            if (_SelectedOrg != null)
                SelectedOrganization = _SelectedOrg;
        }

        /// <summary>
        /// Init organization's FolderType list
        /// </summary>
        private void InitFolderTypeData()
        {
            //Depends of Organization selected item
            if (SelectedOrganization != null)
            {
                FolderTypeComparer L_oFolderTypeComparer = new FolderTypeComparer();
                //List<FolderType> L_oFolderTypeDataSource = new List<FolderType>();
                FolderTypes = new ObservableCollection<FolderType>();
                FolderType _SelectedFolder = null;

                foreach (FolderType FolderTypeInOrga in SelectedOrganization.FolderTypesList)
                {
                    foreach (Group UserGroup in base.CurrentUser.GroupList)
                    {
                        if (Mode == AppDisplayMode.Search)
                        {
                            if ((UserGroup.FolderTypeRigthList.Contains(FolderTypeInOrga, L_oFolderTypeComparer)) && ((UserGroup.CanReorganize || UserGroup.CanConsult)))
                            {
                                FolderTypes.Add(FolderTypeInOrga);
                                if (FolderTypeInOrga.Identifier == GlobalWebContext.CurrentUser.CTX_DefaultType)
                                    _SelectedFolder = FolderTypeInOrga;
                                break;
                            }
                        }
                        else if (Mode == AppDisplayMode.Import || Mode == AppDisplayMode.Flow || Mode == AppDisplayMode.AddDocument)
                        {
                            if ((UserGroup.FolderTypeRigthList.Contains(FolderTypeInOrga, L_oFolderTypeComparer)) && ((UserGroup.CanScann || UserGroup.CanMassImport)))
                            {
                                FolderTypes.Add(FolderTypeInOrga);
                                if (FolderTypeInOrga.Identifier == GlobalWebContext.CurrentUser.CTX_DefaultType && Mode != AppDisplayMode.AddDocument)
                                    _SelectedFolder = FolderTypeInOrga;
                                break;
                            }
                        }
                        else if (Mode == AppDisplayMode.Admin)
                        {
                            if ((UserGroup.FolderTypeRigthList.Contains(FolderTypeInOrga, L_oFolderTypeComparer)) && ((UserGroup.IsAdministrator)))
                            {
                                FolderTypes.Add(FolderTypeInOrga);
                                if (FolderTypeInOrga.Identifier == GlobalWebContext.CurrentUser.CTX_DefaultType)
                                    _SelectedFolder = FolderTypeInOrga;
                                break;
                            }
                        }
                    }
                }

                //Databinding & default selection
                if (_SelectedFolder != null)
                    SelectedFolderType = _SelectedFolder;

                OnFolderTypeSourceChanged();
            }
        }

        /// <summary>
        /// The selected organization change
        /// We have to reload the corresponding folder type
        /// </summary>
        private void cbEntity_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedOrganization = (Organization)cbOrganization.SelectedItem;
            if (SelectedOrganization != null)
                this.SelectedOrganizationIdentifier = SelectedOrganization.Identifier;
            OnOrganizationSelected();
            InitFolderTypeData();
            if (this.Mode != AppDisplayMode.AddDocument)
                cbFolderType.IsEnabled = true;
        }

        /// <summary>
        /// The selected organization change
        /// We have to load indexes controls
        /// </summary>
        private void cbFolderType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedFolderType = (FolderType)cbFolderType.SelectedItem;
            if (SelectedFolderType != null)
                this.SelectedFolderIdentifier = SelectedFolderType.Identifier;
            OnFolderSelected();
        }

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    public class OrganizationSelectedEventArgs : EventArgs
    {
        public Organization Organization { get; private set; }
        public OrganizationSelectedEventArgs(Organization Organization)
        {
            this.Organization = Organization;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class FolderSelectedEventArgs : EventArgs
    {
        public FolderType FolderType { get; private set; }
        public FolderSelectedEventArgs(FolderType FolderType)
        {
            this.FolderType = FolderType;
        }
    }
}



